package servlet;

import Data.data;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/freshdata")
public class Freshdata extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        dofreshdata(request, response);
    }

    private void dofreshdata(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ArrayList<data> dataList = new ArrayList<>();
//        String getid = request.getParameter("id");
//        String getname = request.getParameter("name");
        //  创建HBase配置对象
        Configuration configuration = HBaseConfiguration.create();
        // 指定zookeeper集群地址
        configuration.set("hbase.zookeeper.quorum", "192.168.116.11");
        // 创建连接对象
        Connection connection = ConnectionFactory.createConnection(configuration);
        // 获取Table对象,Table负责与记录相关的操作,如增删改查等
        Table table = connection.getTable(TableName.valueOf("goodsmessage"));

//        for (i=1; i<=10; i++) {
//         Get get = new Get(String.valueOf(i).getBytes());
//                Get get = new Get(Bytes.toBytes(i));
//                Result result = table.get(get);
//
//            Cell[] cells = result.rawCells();
//            String goodsid = new String(CellUtil.cloneValue(cells[0]));
//            String goodsname =  new String(CellUtil.cloneValue(cells[1]));
//            String goodsprice = new String(CellUtil.cloneValue(cells[2]));
//            String goodstime =  new String(CellUtil.cloneValue(cells[3]));
//            String goodsstatus =  new String(CellUtil.cloneValue(cells[4]));
//            data goodsmessage = new data();
//            goodsmessage.setGoodsid(goodsid);
//
//            goodsmessage.setGoodsname(goodsname);
//            goodsmessage.setGoodsprice(goodsprice);
//            goodsmessage.setGoodstime(goodstime);
//            goodsmessage.setGoodsstatus(goodsstatus);
//            dataList.add(goodsmessage);
        Scan scan = new Scan();
        scan.setBatch(100);
        ResultScanner result = table.getScanner(scan);

        for (Result rs : result) {
            String a = null;
            String b = null;
            String c = null;
            String d = null;
            String e = null;
            for (KeyValue kv : rs.raw()) {

                if (new String(kv.getQualifier()).equals("id")) {
                    a = new String(kv.getValue());

                } else if (new String(kv.getQualifier()).equals("name")) {
                    b = new String(kv.getValue());
                } else if (new String(kv.getQualifier()).equals("price")) {
                    c = new String(kv.getValue());
                } else if (new String(kv.getQualifier()).equals("time")) {
                    d = new String(kv.getValue());
                } else if (new String(kv.getQualifier()).equals("status")) {
                    e = new String(kv.getValue());
                }

                data goodsmessage = new data();
                goodsmessage.setGoodsid(a);
                goodsmessage.setGoodsname(b);
                goodsmessage.setGoodsprice(c);
                goodsmessage.setGoodstime(d);
                goodsmessage.setGoodsstatus(e);
                dataList.add(goodsmessage);

            }

        }
        request.setAttribute("goodmessage", dataList);
        request.getRequestDispatcher("/manage9.jsp").forward(request, response);
    }
}
